$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'water/errorreport1/list',
        datatype: "json",
        colModel: [
            { label: 'unkey',  name: 'unkey', index: 'unkey', width: 50,hidden: true,key:true },
            { label: '区域', name: 'areaName', index: 'area_name', width: 70 },
            { label: '系统类型', name: 'deviceType', index: 'device_type', width: 70 },
			{ label: '小区名称', name: 'proName', index: 'pro_name', width: 120 },
			{ label: '数据日期', name: 'dataDate', index: 'data_date', width: 80 },
			{ label: '小区户数', name: 'deviceCount', index: 'device_count', width: 70 },
			{ label: '正常户数', name: 'deviceOk', index: 'device_ok', width: 70 },
			{ label: '修复户数', name: 'deviceFixed', index: 'device_fixed', width: 70 },
			{ label: '新增故障', name: 'errorNew', index: 'error_new', width: 70 },
			{ label: '故障户数', name: 'errorStay', index: 'error_stay', width: 70 },
			{ label: '板子故障', name: 'errorBoard', index: 'error_board', width: 70 },
            { label: '项目经理', name: 'debugerName', index: 'debuger_name', width: 80 },
            { label: '竣工日期', name: 'installerName', index: 'installer_name', width: 80 },
        ],
		viewrecords: true,
        height: window.innerHeight-180 > 385 ? window.innerHeight-180 : 385,
        rowNum: 20,
		rowList : [10,30,50,80,100,150,200,300,500,1000],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        sortname:'data_Date desc,device_type desc,area_name',
        sortorder:'desc',
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        ondblClickRow: function(rowid, iRow, iCol, e){
            $("#jqGrid").jqGrid('resetSelection');
            //$("#jqGrid").jqGrid('setSelection',$("#selRow").val(),false);
            $("#jqGrid").jqGrid('setSelection',rowid);

            vm.openInfoLayer($("#jqGrid").jqGrid("getRowData", rowid));
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });

    $("#jqGridErr").jqGrid({
        url: baseURL + 'water/taccountmanage/listerr',
        datatype: "json",
        postData:{proName:'',dataDate:''},
        colModel: [
            { label: '客户编码', name: 'desc7', index: 'desc7', width: 80 },
            { label: '客户姓名', name: 'desc8', index: 'desc8', width: 120 },
            { label: '客户地址', name: 'desc4', index: 'desc4', width: 220 },
            { label: '设备编码', name: 'desc6', index: 'desc6', width: 120 },
            { label: '设备地址', name: 'desc5', index: 'desc5', width: 120 },
            { label: '当日状态', name: 'desc2', index: 'desc2', width: 80 },
            { label: '上次状态', name: 'desc3', index: 'desc3', width: 80 },
            { label: '采集日期', name: 'desc1', index: 'desc1', width: 160 },
            { label: '采集数据', name: 'desc9', index: 'desc9', width: 80 },
            { label: '质保开始日期', name: 'desc10', index: 'desc10', width: 80 },
            { label: '现场记录', name: 'desc11', index: 'desc11', width: 80 },
            { label: 'memo2', name: 'desc12', index: 'desc12', width: 80 },

        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50,80,100],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: false,
        pager: "#jqGridPagerErr",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGridErr").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });

    $(function () {
        $('#inputClear').click(function () {
            $('input')[0].value = "";
            vm.q.value="";
        })
    });
});

function zTreeOnDblClick(event, treeId, treeNode) {
    vm.q.dept = treeNode.deptId;
    vm.newpage = true;
    vm.reload();
};

var settingLeft = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl"
        }
    },
    callback: {
        onDblClick: zTreeOnDblClick
    }
};

var ztree;

var vm = new Vue({
	el:'#rrapp',
	data:{
        q:{
            key: '',
            value:null,
            dept:null,
        },
        newpage : false,
        searchKeyDictInfo: {},

		showList: true,
		title: null,
		errorReport: {},


	},
	methods: {
        getDeptTreeLeft: function(){
            $.get(baseURL + "water/waterdept/queryDeptAll", function(r){
                //console.log(r);
                ztree = $.fn.zTree.init($("#deptTreeLeft"), settingLeft, r);
                fuzzySearch('deptTreeLeft','#treeSearchKey',null,true);
                var node = ztree.getNodeByParam("deptId", vm.q.dept);
                if(node != null){
                    ztree.selectNode(node);
                    vm.q.dept = node.deptId;
                }
                var temp=ztree.getNodes();
                getNum(ztree,temp);
            })
        },
		query: function () {
            if(vm.q.value != null && vm.q.value != ""
                && (vm.q.key == null || vm.q.key == "")){
                alert('请选择查询条件!');
                return;
            }
            vm.newpage=true;

			vm.reload();
		},
        getSearchKeyDict: function(){
            $.get(baseURL + "sys/dict/list",{name:"qkey_report_err"}, function(r){
                vm.searchKeyDictInfo = r.page.list;
            });
        },
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.errorReport = {};
		},
		update: function (event) {
			var proName = getSelectedRow();
			if(proName == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(proName)
		},
		saveOrUpdate: function (event) {
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = vm.errorReport.proName == null ? "water/errorreport/save" : "water/errorreport/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.errorReport),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
		del: function (event) {
			var proNames = getSelectedRows();
			if(proNames == null){
				return ;
			}
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "water/errorreport/delete",
                        contentType: "application/json",
                        data: JSON.stringify(proNames),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
		getInfo: function(proName){
			$.get(baseURL + "water/errorreport1/info/"+proName, function(r){
                vm.errorReport = r.errorReport;
            });
		},
        outxls: function(){
            var total = $("#jqGrid").jqGrid('getGridParam', 'records') + "";
            var dept = vm.q.dept==null ? "" : vm.q.dept+  "";
            var downUrl = baseURL
                + "water/errorreport1/export?params="
                + encodeURIComponent(JSON.stringify({key:vm.q.key,value:vm.q.value,dept:dept,limit:total,page:"1"}));

            //console.log(decodeURIComponent(url));
            location.href = downUrl;
        },
        getzip: function() {
            var dept = vm.q.dept==null ? "" : vm.q.dept+  "";

            var url = baseURL
                + "water/errorreport1/download?params="
                + encodeURIComponent(JSON.stringify({'dept':dept,page:"1"}));

            location.href = url;
        },
        openInfoLayer :function(event){
            var detailGridName = '#jqGridErr';
            //var page = $(detailGridName).jqGrid('getGridParam','page');
            //if (vm.newpage == true) page = 1;
            $(detailGridName).jqGrid('setGridParam',{
                postData:{'proName': event.proName,'dataDate':event.dataDate},
                page:1
            }).trigger("reloadGrid");

            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "详细信息 【"+event.proName + "】【新增故障：" +event.errorNew+"】【已有故障："+event.errorStay+"】【板子故障："+event.errorBoard+"】",
                area: ['95%', '600px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#infoLayer"),
                btn: ['关闭'],
            });
        },
		reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            if(vm.newpage){page = "1";}
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'key': vm.q.key,'value':vm.q.value,'dept':vm.q.dept},
                page:page
            }).trigger("reloadGrid");
		},
	},
    created: function(){
        this.getSearchKeyDict();
        this.getDeptTreeLeft();
    }
});